<?php
    error_reporting(0);
    define('__ROOT__', dirname(dirname(__FILE__)));
    require_once(__ROOT__.'\config\config.php'); 
    require_once(__ROOT__.'\model\db.php');
    require_once(__ROOT__.'\model\bill_detail.php');
    require_once(__ROOT__.'\model\bill.php');
    require_once (__ROOT__.'\controller\pagination.php');
    $params = array(
        'offset' => isset($_GET['page']) ? (int) $_GET['page'] : 1
    );
    if(!isset($_GET['status']))
    {
        print'<script type="text/javascript">';
        print'location.href = "error.php";';
        print'</script>';
    }
    else
    {
        if($_GET['status'] < 0 || $_GET['status'] > 1)
        {
            print'<script type="text/javascript">';
            print'location.href = "error.php";';
            print'</script>';
        }
        $bill = new Bill();
        $bills = $bill->selectid($_GET['id']);
        if($_GET['status'] != $bills[0]['status'])
        {
            print'<script type="text/javascript">';
            print'location.href = "error.php";';
            print'</script>';
        }
    }
    $bill = new Bill_detail();
    $bills = $bill->select($params,$_GET['id']);
    $total = $bill->count($_GET['id']);
   
?>
<!doctype html>
<html lang="en">

<head>
	<meta charset="utf-8"/>
	<title>Dashboard I Admin Panel</title>
	
	<link rel="stylesheet" href="../css/layout.css" type="text/css" media="screen" />
	<!--[if lt IE 9]>
	<link rel="stylesheet" href="css/ie.css" type="text/css" media="screen" />
	<script src="http://html5shim.googlecode.com/svn/trunk/html5.js"></script>
	<![endif]-->
	<script src="../js/jquery-1.5.2.min.js" type="text/javascript"></script>
	<script src="../js/hideshow.js" type="text/javascript"></script>
	<script src="../js/jquery.tablesorter.min.js" type="text/javascript"></script>
	<script type="text/javascript" src="../js/jquery.equalHeight.js"></script>
</head>


<body>
	<?php
            require_once(__ROOT__.'\view\admin_header.php'); 
        ?>
	
	<section id="main" class="column">
             <?php
                if(isset($_GET['action']))
                {
                    if($_GET['action'] == 'succeed')
                        echo '<h4 class="alert_success">Xác nhận thành công</h4>';
                    if($_GET['action'] == 'fail')
                        echo '<h4 class="alert_error">Số lượng sản phẩm không đủ</h4>';
                }
            ?>
		<article class="module width_3_quarter">
		<header>
		<ul class="tabs">
                    <?php
                        $pagination = new Pagination();
                        $params = array_merge($params, array(
                            'current_page' => isset($_GET['page'])?$_GET['page']:1,
                            'total_page' => $total,
                            'file' => 'view_billdetail.php?id='.$_GET['id'].'&status='.$_GET['status']
                        ));
                        echo $pagination->display2($params);
                    ?>
		</ul>
		</header>
		<div class="tab_container">
			<div id="tab1" class="tab_content">
			<table class="tablesorter" cellspacing="0"> 
			<thead> 
				<tr> 
    				<th>Sản Phẩm</th> 
    				<th>Số Lượng</th> 
    				<th>Chi Phí</th> 
                                <th>Biên Tập</th> 
				</tr> 
			</thead> 
                         <?php foreach($bills as $billss): ?>
			<tbody>  
                                <tr> 		
                                    <td><?php echo $billss['nameproduct'] ?></td>
                                    <td><?php
                                            if($_GET['status'] == 1)
                                                echo $billss['quantity'];
                                            else
                                                echo '<form method="post" action="'.HOST.'controller/billdetail_controller.php?id='.$billss["id"].'&totalcost='.$billss["totalcost"].'&idbill='.$billss["idbill"].'&price='.$billss["totalcost"]/$billss["quantity"].'"><input type="text" style="width: 30%; text-align: center" value="'.$billss["quantity"].'" name="quantity"></form>';
                                        ?>
                                    </td> 
                                    <td><?php echo number_format($billss['totalcost'] , 0, ',', '.'); ?> Đồng</td> 
                                    
                                    <td>
                                        <?php
                                            if($_GET['status'] == 1)
                                                echo '<input type="image" src="'.HOST.'images/admin/icn_alert_warning.png" title="Không thể sửa">';
                                            else
                                            {
                                                $var = "window.location.href = '".HOST."controller/billdetail_controller.php?action=delete&id=".$billss['id']."&totalcost=".$billss['totalcost']."&idbill=".$billss['idbill']."'";                                           
                                                echo '<input type="image" src="'.HOST.'images/admin/icn_logout.png" title="Xóa" onclick="'.$var.'">';
                                            }
                                        ?>
                                    </td>
				</tr> 
			</tbody> 
                        <?php endforeach; ?>
			</table>
			</div>
		</div>
		
		</article>
	</section>


</body>

</html>